<%@ page contentType="text/html; charset=UTF-8" errorPage="/jsp/error.jsp" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld"  prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld"  prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/tlds/custom-html.tld" prefix="chtml" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.erp.app.util.web.WebUtil" %>
<%@ page import="com.erp.framework.util.CommonUtil" %>
<%@ page import="com.erp.app.common.ProjectConstants" %>
<%@ page import="com.erp.app.report.genericInquiry.inquiryMaster.beans.*" %>
<%@ page import="com.erp.app.report.genericInquiry.inquiryDisplay.beans.vo.*" %>
<%@ page import="com.erp.app.report.genericInquiry.inquiryCriteria.beans.vo.*" %>
<%@ page import="com.erp.app.report.genericInquiry.*" %>
<%@ page import="com.erp.app.report.genericInquiry.GenericInquiryConstants" %>
<%@ page import="org.apache.struts.action.ActionErrors" %>
<%@ page import="com.erp.framework.util.user.User" %>

<jsp:useBean id="genericInquirySearchForm" scope="session" type="GenericInquirySearchForm" />
<bean:define id="masterVO" name="genericInquirySearchForm" property="genericInquiryMasterVO" type="GenericInquiryMasterVO"/>
<%	ArrayList list = genericInquirySearchForm.getResultList();
	pageContext.setAttribute("list", list); 
	
	ArrayList reportList = (ArrayList)genericInquirySearchForm.getReportResultList();
	if (reportList != null)
		pageContext.setAttribute("reportResultList", reportList);
	
	ArrayList displayList = masterVO.getLocalChilds(InquiryDisplayVO.class);
	if (displayList != null)
		pageContext.setAttribute("displayList", displayList); 
		 	
 	ArrayList critList = masterVO.getLocalChilds(InquiryCriteriaVO.class);
	if (critList != null)
		pageContext.setAttribute("critList", critList); 

	String lastAction = CommonUtil.nullToString(genericInquirySearchForm.getPageEvent());
%>
<script language="javascript">
	var hidden = false;
	
	document.body.onkeydown = checkKey;

	function checkKey() {
		var code = window.event.keyCode;
		
		if (code == 13)
			return false;
	}
	
	function doSubmit(event, src) {
		populateLabels();
	    if (event) document.forms[0].pageEvent.value = event;	
	    if (src) document.forms[0].pageEventSrc.value = src;
	    isSubmitted = 1;
	    parseMDD();
    	displaySearchMsg();
    	doProtect("srch_btn");
		doProtect("srch_crit_input_tbl");
	    document.forms[0].submit();
	}
	
	function populateLabels() {
		var elms = document.getElementsByTagName("select");
		var s;
		var idx;
		var value;

		for (var i = 0; i < elms.length; i++) {
			s = elms[i];
			idx = s.selectedIndex;
			if (idx > -1) {
				if (s.multiple)
					value = concatArrayValues(s.options, "text", "selected", "<Br>");
				else 
					value = s.options[idx].text;

				var obj = findClosestElementFromSrc(s, "input", "srchFromLabel");
				obj.value = value;
			}
		}
	}

	function findClosestElementFromSrc(src, tag, name) {
		var tags;
		
		while (src.parentNode) {
			src = src.parentNode;
			tags = src.getElementsByTagName(tag);

			for (var i = 0; i < tags.length; i++) {
				if (tags[i].name == name)
					return tags[i];
			}			
		}
		
		return;
	}

	function concatArrayValues(array, attribute, optional, delimiter) {
		var ele;
		var rslt = "";
		var read = false;
		
		if (typeof(delimiter) == "undefined")
			delimiter = "<%=GenericInquiryConstants.DELIMITER_MULTIPLE_DROP_DOWN%>";

		for (var i = 0; i < array.length; i++) {
			ele = array[i];
			read = (typeof(optional) != "undefined" && ele.getAttribute(optional)) ||
					typeof(optional) == "undefined";
			if(read)
				rslt += rslt.length > 0 ? delimiter + ele.getAttribute(attribute) : ele.getAttribute(attribute);		
		}
		return rslt;
	}
	
	function parseMDD() {
		var elms = document.getElementsByName("MDD");
		var value = "";
		var select;
		var idx;
		if (elms) {
			for(var i = 0; i < elms.length; i++) {
				select = elms[i];
				value = concatArrayValues(select.options, "value", "selected", "<%=GenericInquiryConstants.DELIMITER_MULTIPLE_DROP_DOWN%>");
				idx = select.name;
				document.getElementsByName("srchFrom")[idx].value = value;
			}
		}
	}
	
	function doLoad() {	
		autoSelectMDDOptions();
		<%	if (lastAction.equalsIgnoreCase("popupSearch")) {
				out.println(WebUtil.openSearchDo());
			} else if ((request.getAttribute(org.apache.struts.action.Action.ERROR_KEY) == null)
					&& (lastAction.equalsIgnoreCase("createReport") 
					|| lastAction.equalsIgnoreCase("printPdf")
					|| lastAction.equalsIgnoreCase("createTxt")
					|| lastAction.equalsIgnoreCase("createHtmlTxt")
					)) {
				out.println("document.forms[0].submit();");
			}
		%>	
	}
	
	function autoSelectMDDOptions() {
		var elms = document.getElementsByName("MDD");
		var value = "";
		var idx = -1;
		var strArray;
		var count = 0;
		var select;
		var option;

		if (elms) {
			for (var i = 0; i < elms.length; i++) {
				select = elms[i];
				idx = select.name;
				value = document.getElementsByName("srchFrom")[idx].value;
				strArray = value.split("<%=GenericInquiryConstants.DELIMITER_MULTIPLE_DROP_DOWN%>");
				count = 0;
				for (var j = 0; j < select.options.length; j++) {
					option = select.options[j];
					if (option.value == strArray[count] && strArray[count] != "") {
						count++;
						option.selected = true;
					} else
						option.selected = false;
				}
			}
		}
	}
	
	function toggleHidden() {
		var elms = document.getElementsByName("TAGGED_HIDDEN");
		if (elms) {
			for (var i = 0; i < elms.length; i++) {
				if (hidden)
					elms[i].style.display = "block";
				else
					elms[i].style.display = "none";
			}
		}
		var t = document.getElementById("src_table");
		if (hidden)
			t.style.width = "<%= genericInquirySearchForm.getTableWidth() %>px";
		else
			t.style.width = "<%= genericInquirySearchForm.getTableWidth(true) %>px";
		hidden = !hidden;
	}	
	
	function setMultiBoxLabel(eIdx,cbName) {
		var labelValue = "";
		var elms = document.getElementsByName(cbName);
		if (elms) {
			for (var i=0; i<elms.length; i++) {
				if (elms[i].checked) {
					labelValue += (labelValue.length > 0 ? "<%=GenericInquiryConstants.DELIMITER_MULTIPLE_DROP_DOWN%>" : "") + elms[i].caption;
				}
			}
		}
		elms = document.getElementsByName("srchFromLabel");
		if (elms) {
			elms[eIdx].value = labelValue;
		}
	}
	
	<!-- Parameters used in displaying waiting msg -->
	var max_msg_loop = 20;
	var msg_counter = 0;
	var interval_time = 100;
	var trailing_character = ". ";
	var event_search_msg_mapping = ["search", "createReport", "printPdf", "createTxt", "createHtmlTxt"];
	var search_msg_prefices = ["Searching In Progress", "Downloading Excel In Progress", "Downloading Pdf In Progress", "Downloading Txt In Progress", "Downloading Html Txt In Progress"];
	var search_text = "";
	var interval;
	
	function displaySearchMsg() {
		var ele = document.getElementById("searchMsg");

		if (interval) {
			clearInterval(interval);
			ele.style.display = "none";
		}
			
		var event = document.forms[0].pageEvent.value;
		if (getEventSearchTextMapping(event) == -1)
			return;
			
		var content = document.getElementById("searchResultContent");
		content.style.display = "none";
		ele.style.display = "block";
		
		
		interval = setInterval('buildSearchMsg()', interval_time);
	}
	
	function getEventSearchTextMapping(event) {
		var idx = -1;
		
		for (var i = 0; i < event_search_msg_mapping.length; i++) {
			if (event == event_search_msg_mapping[i]) {
				idx = i;
				break;
			}
		}
		
		return idx;
	}
	
	function buildSearchMsg() {
		var event = document.forms[0].pageEvent.value;
		var ele = document.getElementById("searchMsg");
		var prefix = "";
		var idx = getEventSearchTextMapping(event);
		
		if (idx == -1)
			prefix = "[N/A]";
		else
			prefix = search_msg_prefices[idx];
		
		if (msg_counter == 0)
			search_text = prefix + " ";
		search_text += trailing_character;
		msg_counter++;
		
		if (msg_counter > max_msg_loop)
			msg_counter = 0;
		
		ele.innerHTML = search_text;
	}
	
	function showCrit() {
		var e1 = document.getElementById("srch_crit_tbl");
		e1.style.display = "none";
		var e2 = document.getElementById("srch_crit_input_tbl");
		e2.style.display = "block";
		var e3 = document.getElementById("searchResultContent");
		e3.style.display = "none";
	}
	
	function doProtect(id) {
		var e = document.getElementById(id);
		var elms = e.getElementsByTagName("input");
		
		for (var i = 0; i < elms.length; i++) {
			if (elms[i].type == 'button')
				elms[i].disabled = "true";
		}
	}

	var parse_form = "genericInquirySearchForm";	

	function redirect(url) {
		var r_qs = parseURL(url);
		var f = eval("document." + parse_form);

		f.action = r_qs;
		f.submit();
	}
/*
	function parseURL(url) {
		var qs_i = url.indexOf('?');	
		
		if (qs_i == -1)
			return url;

		var r = url.substr(0, qs_i);
		var qs = url.substr(qs_i + 1, url.length);
		var qs_ele = qs.split("&");
		var rtn_val = "";

		for (var i = 0; i < qs_ele.length; i++) {
			var p = qs_ele[i].split("=");
			
			if (p.length != 2) // value pair is malformed
				continue;

			var obj = eval("document." + parse_form + "." + p[0]);

			if (typeof(obj) == 'undefined') {
				if (rtn_val.length > 0)
					rtn_val += "&";
				rtn_val += common_urlEncode(p[0]) + "=" + common_urlEncode(p[1]);
			} else
				obj.value = p[1];						
		}

		return rtn_val.length == 0 ? r : r + "?" + rtn_val;		
	}
*/

	function parseURL(url) {
		var qs_i = url.indexOf('?');	
		
		if (qs_i == -1)
			return url;

		var r = url.substr(0, qs_i);
		var qs = url.substr(qs_i + 1, url.length);
		var qs_ele = qs.split("&");
		var rtn_val = "";

		for (var i = 0; i < qs_ele.length; i++) {
			var p = qs_ele[i].split("=");
			
			if (p.length != 2) // value pair is malformed
				continue;

			var obj = eval("document." + parse_form + "." + p[0]);

			if (typeof(obj) == 'undefined') {
				if (rtn_val.length > 0)
					rtn_val += "&";
				rtn_val += qs_ele[i];
			} else
				obj.value = p[1];						
		}

		return rtn_val.length == 0 ? r : r + "?" + rtn_val;		
	}
</script>
<body onLoad="javascript:doLoad();autofill_init('', 'From');">
<html:form action="/genericInquiry">
<html:hidden property="pageEvent" />
<html:hidden property="pageMode" />
<html:hidden property="pageEventSrc"/>

<!-- Search Criteria -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
	<td>
		<div id="srch_btn">
			<chtml:button form="genericInquirySearchForm" pageEvent="exit" styleClass="button1" />
			<logic:present name="critList">
				<chtml:button form="genericInquirySearchForm" pageEvent="clear" styleClass="button1" />		
				<chtml:button form="genericInquirySearchForm" pageEvent="search" styleClass="button1" />
				<chtml:button form="genericInquirySearchForm" pageEvent="createReport" styleClass="button1" />
				<chtml:button form="genericInquirySearchForm" pageEvent="printPdf" styleClass="button1" />
				<chtml:button form="genericInquirySearchForm" pageEvent="createTxt" styleClass="button1" />
				<chtml:button form="genericInquirySearchForm" pageEvent="createHtmlTxt" styleClass="button1" />				
			</logic:present>
		</div>
	</td>
</tr>
</table>

<jsp:include page="genericInquirySearchCriteria.jsp" flush="true"/>

<div id="searchMsg" style="font-size:15pt;font-weight:bold;color:#3333FF;display:none"></div>

<div id="searchResultContent" style="display:block">
	<font color='red'><html:errors /></font>
	<% // if(list != null && list.size() == 500){ %>
	    <%--
	    <font color='red'><ul><bean:message key="message.search.warning" /></ul></font> 
	    --%>
	<% //} %>
	<% if(ProjectConstants.GENERIC_YES.equals(genericInquirySearchForm.getExceedMaxRslt())){%>
	  <font color='red'><ul><bean:message key="message.genericInquiry.search.warning" /></ul></font>
    <%} %>
	<logic:present name="displayList">
		<logic:notPresent name="genericInquirySearchForm" property="reportResultList">
			<div  id="toggleHidden" style="display:block;padding:10px 0px 10px 0px" class="t2">
				<input type="button" value="Toggle Hidden" onclick="javascript:toggleHidden()" class="button1">
				<span class="t2" style="font-weight:bold;color:#000000">&nbsp;*Only affect labels coloured in yellow*</span>
			</div>
						<jsp:include page="genericInquirySearchScrolls.jsp" flush="true"/>		
				
			<table id="src_table" cellspacing="1" cellpadding="0" style="line-height:18px;<%=genericInquirySearchForm.getTableWidth().equals("0") ? "" : "width:" + genericInquirySearchForm.getTableWidth() + "px"%>" class="table_grid">
				
				<jsp:include page="genericInquiryTableHeaders.jsp" flush="true"/>
				<jsp:include page="genericInquirySearchResult.jsp" flush="true"/>
			</table>
		</logic:notPresent>
	</logic:present>
</div>

</html:form>